import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data
import torch.optim as optim
import numpy as np
import matplotlib.pyplot as plt
import torchvision
import os
import shutil
import copy
from torchvision import transforms


base_dir = 'D:/learn/深度学习/四种天气图片数据集/四种天气图片数据集/dataset2'

train_dir = os.path.join(base_dir, "train")
test_dir = os.path.join(base_dir, "test")
filenames = os.listdir(base_dir)
print(len(filenames))
species = ['cloudy', 'rain', 'shine', 'sunrise']

if not os.path.exists(train_dir):
    os.mkdir(train_dir)
if not os.path.exists(test_dir):
    os.mkdir(test_dir)

for train_or_test in ['train', 'test']:
    for spec in species:
        path = os.path.join(base_dir, train_or_test, spec)
        if not os.path.exists(path):
            os.mkdir(path)

for i, img in enumerate(filenames):
    for spec in species:
        if img.startswith(spec):
            img_path = os.path.join(base_dir, img)
            if i%5 == 0:
                path = os.path.join(base_dir, "test", spec, img)
            else:
                path = os.path.join(base_dir, 'train', spec, img)
            if not os.path.exists(path):
                print(img_path)
                shutil.copy(img_path, path)

for train_or_test in ['train', 'test']:
    for spec in species:
        fileSize = len(os.listdir(os.path.join(base_dir, train_or_test, spec)))
        print(train_or_test, spec, fileSize)

transform = transforms.Compose([
    transforms.Resize((96, 96)),
    transforms.ToTensor(),
    #正则化
    transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
])

train_ds = torchvision.datasets.ImageFolder(train_dir, transform=transform)
test_ds = torchvision.datasets.ImageFolder(test_dir, transform=transform)
print(test_ds.classes)
batch_size = 32
train_dl = torch.utils.data.DataLoader(train_ds, batch_size=batch_size, shuffle=True)
test_dl = torch.utils.data.DataLoader(test_ds, batch_size=batch_size*4)

imgs, labels = next(iter(train_dl))
img = (imgs[0] + 1)/2
#显示一张图片
plt.imshow(img.permute(1, 2, 0))
#plt.show()

class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(3, 16, 3) #16*94*94
        self.bn1 = nn.BatchNorm2d(16)
        self.pool = nn.MaxPool2d(2, 2) #16*47*47
        self.conv2 = nn.Conv2d(16, 32, 3) #32*45*45 -> pooling -> 32*32*32
        self.bn2 = nn.BatchNorm2d(32)
        self.conv3 = nn.Conv2d(32, 64, 3)  #64*20*20 -> pooling -> 64*10*10
        self.bn3 = nn.BatchNorm2d(64)
        self.dropout = nn.Dropout()

        #batch,
        self.fc1 = nn.Linear(64*10*10, 1024)
        self.bn_fc1 = nn.BatchNorm1d(1024)
        self.fc2 = nn.Linear(1024, 256)
        self.bn_fc2 = nn.BatchNorm1d(256)
        self.fc3 = nn.Linear(256, 4)

    def forward(self, x):
        x = self.pool(self.bn1(self.conv1(x)))
        x = self.pool(self.conv2(x))
        x = self.pool(self.conv3(x))

        x = nn.Flatten()(x)
        x = F.relu(self.fc1(x))
        x = self.dropout(x)
        x = F.relu(self.fc2(x))
        x = self.dropout(x)
        x = self.fc3(x)
        return x

model = Net()

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
model.to(device)
optimizer = optim.Adam(model.parameters(), lr=0.001)
loss_fn = nn.CrossEntropyLoss()


def fit(epoch, model, train_loader, test_loader):
    correct = 0
    total = 0
    running_loss = 0
    model.train()
    for x, y in train_loader:
        # 把数据放到GPU上去
        x, y = x.to(device), y.to(device)
        y_pred = model(x)
        loss = loss_fn(y_pred, y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        with torch.no_grad():
            y_pred = torch.argmax(y_pred, dim=1)
            correct += (y_pred == y).sum().item()
            total += y.size(0)
            running_loss += loss.item()

    epoch_loss = running_loss / len(train_loader.dataset)
    epoch_acc = correct / total

    # 测试过程
    test_correct = 0
    test_total = 0
    test_running_loss = 0
    model.eval()
    with torch.no_grad():
        for x, y in test_loader:
            x, y = x.to(device), y.to(device)
            y_pred = model(x)
            loss = loss_fn(y_pred, y)
            y_pred = torch.argmax(y_pred, dim=1)
            test_correct += (y_pred == y).sum().item()
            test_total += y.size(0)
            test_running_loss += loss.item()

    test_epoch_loss = test_running_loss / len(test_loader.dataset)
    test_epoch_acc = test_correct / test_total

    print('epoch:', epoch,
          'loss:', round(epoch_loss, 3),
          'accuracy:', round(epoch_acc, 3),
          'test_loss:', round(test_epoch_loss, 3),
          'test_accuracy:', round(test_epoch_acc, 3)
          )

    return epoch_loss, epoch_acc, test_epoch_loss, test_epoch_acc


epochs = 20
"""
train_loss = []
train_acc = []
test_loss = []
test_acc = []
for epoch in range(epochs):
    epoch_loss, epoch_acc, test_epoch_loss, test_epoch_acc = fit(epoch, model, train_dl, test_dl)
    train_loss.append(epoch_loss)
    train_acc.append(epoch_acc)
    test_loss.append(test_epoch_loss)
    test_acc.append(test_epoch_acc)

torch.save(model.state_dict(), "./modelSave/weather.pth")
new_model = Net()
new_model.load_state_dict(torch.load("./modelSave/weather.pth"))

new_model.to(device)
test_correct = 0
test_total = 0
new_model.eval()

with torch.no_grad():
    for x, y in test_dl:
        x, y = x.to(device), y.to(device)
        y_pred = new_model(x)
        y_pred = torch.argmax(y_pred, dim=1)
        test_correct += (y_pred == y).sum().item()
        test_total += y.size(0)

print(test_correct / test_total)
"""


#保存最优参数
model = Net()
model.to(device)

optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
best_model_weight = model.state_dict()
best_acc = 0.0

train_loss = []
train_acc = []
test_loss = []
test_acc = []

for epoch in range(epochs):
    epoch_loss, epoch_acc, test_epoch_loss, test_epoch_acc = fit(epoch, model, train_dl, test_dl)
    train_loss.append(epoch_loss)
    train_acc.append(epoch_acc)
    test_loss.append(test_epoch_loss)
    test_acc.append(test_epoch_acc)

    if epoch_acc > best_acc:
        best_acc = epoch_acc
        # 更新最优参数
        best_model_weight = copy.deepcopy(model.state_dict())

res = model.load_state_dict(best_model_weight)
print(res)
torch.save(model, "./modelSave/weather_whole.pth")
new_model2 = torch.load("./modelSave/weather_whole.pth", map_location=device)













































